package functionalTest;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;

public class AutotestUtils {
    public static WebDriver webDriver;
    public static String path = "C:\\Program Files\\Google\\Chrome\\Application\\chromedriver.exe";
    // 显示等待确保登录后跳转到指定页面
    public static WebDriverWait wait;

    public void setUp() {
        System.setProperty("webdriver.chrome.driver", path);
        webDriver = new ChromeDriver();
        webDriver.get("http://47.120.16.255:8081/login.html");
        wait = new WebDriverWait(webDriver, Duration.ofSeconds(10));
    }

    public AutotestUtils() {
        setUp();
    }

    // 获取截图时间
    public static List<String> getTime() {
        // 文件格式 20240201-453457毫秒
        SimpleDateFormat sim1 = new SimpleDateFormat("yyyyMMdd-HHmmss");
        String filename = sim1.format(System.currentTimeMillis());
        List<String> list = new ArrayList<>();
        list.add(filename);
        return list;
    }

    // 获取截图结果并保存
    public static void getScreenShot(WebDriver webDriver) throws IOException {
        List<String> list = getTime();
        String filename = "./src/test/java/com/autoTestShot/" + list.get(0) + ".png";
        File srcFile = ((TakesScreenshot)webDriver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(srcFile, new File(filename));
    }
}
